/**
 * @Name:ByteBuff
 * @description:bytebuff
 * @author: xinY
 **/
public class ByteBuff {
    private byte[] data;
    private int index;

    /**
     * 构造方法
     * @param data
     * @param index
     */
    public ByteBuff(byte[] data, int index) {
        this.data = data;
        this.index = index;
    }

    /**
     * 构造方法
     */
    public ByteBuff() {
        init();
    }


    /**
     * 初始化
     */
    public void init(){
        this.data = new byte[1];
        this.index = 0;
    }



    /**
     * 写byte
     * @param b
     * @return
     */
    public ByteBuff add(byte b){
        if (index < data.length){
            this.data[index] = b;
            this.index += 1;
        }else {
            // 超出了length
            byte[] bytes = new byte[this.data.length + 1];
            for (int j = 0 ; j < this.index ; j ++){
                bytes[j] = this.data[j];
            }
            setData(bytes);
            this.data[index] = b;
            this.index += 1;
        }
        return this;
    }

    /**
     * 写int
     * @param i
     * @return
     */
    public ByteBuff add(int i){
        if ( i < 256 && i >= 0){
            add((byte)i);
        }else if( i >= 0 && i < 65536){
            add(intTo2ByteArray(i));
        }
        else {
            add(intTo4ByteArray(i));
        }
        return this;
    }

    /**
     * 写byte[]
     * @param bytes
     * @return
     */
    public ByteBuff add(byte[] bytes){
        for (int i = 0 ; i < bytes.length ; i ++){
            add(bytes[i]);
        }
        return this;
    }

    /**
     * 写入自己
     * @param byteBuff
     * @return
     */
    public ByteBuff add(ByteBuff byteBuff){
        this.add(byteBuff.getData());
        return this;
    }

    public ByteBuff add(String s){
        byte[] bytes = s.getBytes();
        this.add(bytes);
        return this;
    }

    /**
     * 打印
     * @return
     */
    public String print(){
        if ( index < 1 ){
            return "null";
        }
        String s = "[";
        for ( int i = 0 ; i < data.length ; i ++ ){
            s += data[i] + ",";
        }
        s = s + "]";
        return s + "(index):" + index;
    }

    /**
     * 获取下标【i】的byte
     * @param i
     * @return
     */
    public byte get(int i){
        try {
            if( i > this.index){
                throw new RuntimeException("超过了下标");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return this.data[i];
    }

//    public byte[] toArray(){
//        byte[] bytes = new
//    }
    /**
     * 清空自己
     * @return
     */
    public ByteBuff clear(){
        this.data = new byte[this.index];
        this.index = 0;
        return this;
    }



    /**
     * 一个int 转成byte[]
     * @param i
     * @return
     */
    public  byte[] intTo4ByteArray(int i) {
        byte[] result = new byte[4];
        result[0] = (byte)((i >> 24) & 0xFF);
        result[1] = (byte)((i >> 16) & 0xFF);
        result[2] = (byte)((i >> 8) & 0xFF);
        result[3] = (byte)(i & 0xFF);
        return result;
    }

    /**
     * 一个 short转成byte[]
     * @param i
     * @return
     */
    private byte[] intTo2ByteArray(int i) {
        byte[] result = new byte[2];
        result[0] = (byte)((i >> 8) & 0xFF);
        result[1] = (byte)(i & 0xFF);
        return result;
    }


    /**
     * setter and getter
     * @param data
     */
    public void setData(byte[] data) {
        this.data = data;
    }

    public byte[] getData() {
        return data;
    }

    /**
     * setter and getter
     * @return
     */
    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }
}